/*
* @Author: ZHELIHAO\fangshuqin
* @Date:   2018-05-15 18:11:10
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-26 19:41:32
*/
{
    let EVENT_CLICK = Laya.Event.CLICK;
    class Basics extends ui.Info.BasicsUI{
        constructor() {
            super();
            this.init();
        }

        init(){
            this.registerActions();
            this.initConfig();
            this.initEvent();
        }

          // 初始化配置
        initConfig() {
            this.config = {
                MAX_WIDTH: 240,
                MIN_WIDTH: 25

            }

            this.userInfo = null ;
        }

        initEvent() {
           
            this.btn_modify.on(EVENT_CLICK, this, ()=>{
                
            
                let nick = this.dom_nick.text;
                let sex  = this.sex.index;
                let diamond = this.userInfo.nickDiamond;
                let currentDiamond = this.userInfo.diamond;
                let cb = (nick,sex)=>{
                    this.dom_nick.text = nick;
                    this.sex.index = sex;
                }
                // Sail.director.popScene(new Alert.ModifyNick(0));
                Sail.director.popScene(new Alert.ModifyNick(1, nick, sex, diamond, currentDiamond, cb));
            })

            this.IconBox.on(EVENT_CLICK, this, ()=>{
                Sail.io.emit(GAME_CMDS.USETICONANDFRAMELIST);
                let iconChange = (idx)=>{
                    this.currentIcon.skin =  `res/info/info_${idx}.png`;
                }

                let frameChange = (idx)=>{
                    this.currentIframe.skin =  `res/info/iframe_${idx}.png`;
                }
                Sail.director.popScene(new Alert.IconAndFrame(iconChange,frameChange));
            })
        }

        registerActions() {

            this.ACTIONS = {
                [GAME_CMDS.USE_INFO] : this.userBasicsInfo
            };
            Sail.io.register(this.ACTIONS, this);
        }

        userBasicsInfo(data, code, msg) {
            if(code !==0 ) return;
            this.userInfo = data;
            console.log("userBasicsInfo", data);
            this.currentIcon.skin = `res/info/info_${data.icon}.png`;
            this.currentIframe.skin  = `res/info/iframe_${data.frame}.png`;
            
            this.dom_vip.text = data.vip_lvl;
            this.sex.index = data.sex;
            this.dom_nick.text = data.nick_name;

            let userGrade = Sail.store.getUserGrade(Number(data.grade));
            let userLevel = Sail.store.getUserLevel(Number(data.exp_lvl))

            // 等级
            let exp_progress =  data.exp / userLevel.next_exp;
            let exp_per = Math.floor( exp_progress * 100 );
            let exp_w = exp_per / 100 * this.config.MAX_WIDTH;
            exp_w = exp_w < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : exp_w; 
            this.progress_exp.scrollRect = {x : 0, y : 0, width : exp_w, height : 54};
            this.dom_progress_exp.text = data.exp + '/' + userLevel.next_exp;
            this.expnum.text = data.exp_lvl;

            // 段位
            let grade_mole = data.cup_num - userGrade.cup_num;  //分子
                grade_mole >0 ? grade_mole :0;
            let grade_deno = userGrade.next_cup_num;          //分母
            let grade_progress = grade_mole / grade_deno;

            let grade_per = Math.floor( grade_progress * 100 );
            let grade_w = grade_per / 100 * this.config.MAX_WIDTH;

            grade_w = grade_w < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : grade_w; 

            this.progress_grade.scrollRect = {x : 0, y : 0, width : grade_w, height : 42};
            this.progress_grade.visible = true;

            this.dom_progress_grade.text = userGrade.cup_num + '/' +  userGrade.next_cup_num ;
            this.grade.index = Number(userGrade.icon);
            this.gradestar.index =  Number(userGrade.icon);

            this.dom_union.text = data.guild_id ==0 ? "暂未加入公会" : data.guild_id ;
        }

        exit () {
            Sail.io.unregister(this.ACTIONS);
        }
    }
    Sail.class(Basics, "Com.Info.Basics");
}